#ifndef NVS_MANAGER_H
#define NVS_MANAGER_H

#include "esp_err.h"
#include <stdbool.h>

// device 命名空间
#define NVS_DEVICE_INFO_NAMESPACE       "device_info_namespace"
// device 秘钥
#define NVS_WIFI_INFO_KEY               "wifi_info_key"
#define NVS_ALIOT_INFO_KEY              "aliot_info_key"

// wifi默认数据
#define NVS_WIFI_SSID       "long"
#define NVS_WIFI_PASSWORD   "12345678"

// wifi的NV管理器配置
#define NVS_WIFI_SSID_MAX_LEN       64
#define NVS_WIFI_PASSWORD_MAX_LEN   64

// wifi信息结构体定义
typedef struct {
    char ssid[NVS_WIFI_SSID_MAX_LEN];                    // wifi 名称
    char password[NVS_WIFI_PASSWORD_MAX_LEN];            // wifi 密码
    uint32_t crc32;                                      // 数据校验码
} nv_wifi_info_t;


// aliot 默认数据
#define NVS_ALIOT_DEVICE_NAME        "rgbesp3201"               // 设备名称
#define NVS_ALIOT_REGION_ID          "cn-shanghai"              // YourRegionId 北京:cn-beijing;上海:cn-shanghai;深圳:cn-shenzhen
#define NVS_ALIOT_PRODUCT_KEY        "a1KaEptlOFo"              // 产品KEY
#define NVS_ALIOT_PRODUCT_SECRET     "XBy6j3XshVnffaTO"         // 产品SECRET
#define NVS_ALIOT_DEVICE_SECRET      "91fa3c7183b1be35278ecb59a8e61112" // 设备秘钥

// NV管理器配置
#define NVS_ALIOT_DEVICE_NAME_MAX_LEN        64
#define NVS_ALIOT_REGION_ID_MAX_LEN          64
#define NVS_ALIOT_PRODUCT_KEY_MAX_LEN        64
#define NVS_ALIOT_PRODUCT_SECRET_MAX_LEN     64
#define NVS_ALIOT_DEVICE_SECRET_MAX_LEN      64

// 设备信息结构体定义
typedef struct {
    char device_name[NVS_ALIOT_DEVICE_NAME_MAX_LEN];         // 设备名称
    char region_id[NVS_ALIOT_REGION_ID_MAX_LEN];             // YourRegionId 北京:cn-beijing;上海:cn-shanghai;深圳:cn-shenzhen
    char product_key[NVS_ALIOT_PRODUCT_KEY_MAX_LEN];         // 产品KEY
    char product_secret[NVS_ALIOT_PRODUCT_SECRET_MAX_LEN];   // 产品SECRET
    char device_secret[NVS_ALIOT_DEVICE_SECRET_MAX_LEN];     // 设备秘钥
    uint32_t crc32;                                          // 数据校验码
} nv_aliot_info_t;

// 设备信息结构体定义
typedef struct {
    nv_wifi_info_t wifi_info;
    bool wifi_info_cache_valid;
    nv_aliot_info_t aliot_info;
    bool aliot_info_cache_valid;
} nv_device_info_t;

// 函数外部申明
esp_err_t nvs_device_info_save(const char* key, void* data, size_t size);
esp_err_t nvs_device_info_read(const char* key, void* data);
esp_err_t nvs_manager_init(void);
nv_device_info_t *get_device_info_cache(void);


#endif // NVS_MANAGER_H